package com.demo.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.demo.dao.Result;
import com.demo.pojo.system.Post;
import com.demo.service.system.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/system/post")
public class PostController {

    @Autowired
    private PostService postService;

    @PostMapping
    public Result addPost(@RequestBody Post post) {
        postService.save(post);
        return Result.success();
    }

    @DeleteMapping
    public Result deletePost(@RequestParam int id) {
        postService.removeById(id);
        return Result.success();
    }

    @PutMapping
    public Result updatePost(@RequestBody Post post) {
        postService.updateById(post);
        return Result.success();
    }

    @GetMapping
    public Result getPost(int pageNum, int pageSize,
                          @RequestParam(required = false,defaultValue = "") String postName,
                          @RequestParam(required = false,defaultValue = "") String postCode,
                          @RequestParam(required = false,defaultValue = "") String status) {
        Page<Post> page = postService.lambdaQuery()
                .like(Post::getName,postName)
                .like(Post::getPostCode,postCode)
                .like(Post::getStatus,status)
                .page(Page.of(pageNum, pageSize));
        return Result.success(page);
    }
}
